home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2004 April / Gamestar_61_2004-04_dvdb.iso / DVDStar / Editace / hltp.exe / {app} / Source Code / Zoners Half-Life Tools / common / cmdlib.cpp < prev    next >
C/C++ Source or Header  |  2001-12-24  |  9KB  |  436 lines

  1. #ifdef SYSTEM_WIN32
  2. #define WIN32_LEAN_AND_MEAN
  3. #include <windows.h>
  4. #endif
  5.  
  6. #include "cmdlib.h"
  7. #include "messages.h"
  8. #include "hlassert.h"
  9. #include "blockmem.h"
  10. #include "log.h"
  11.  
  12. #ifdef SYSTEM_POSIX
  13. #ifdef HAVE_SYS_STAT_H
  14. #include <sys/stat.h>
  15. #endif
  16. #ifdef HAVE_FCNTL_H
  17. #include <fcntl.h>
  18. #endif
  19. #ifdef HAVE_UNISTD_H
  20. #include <unistd.h>
  21. #endif
  22. #endif
  23.  
  24. #define PATHSEPARATOR(c) ((c) == '\\' || (c) == '/')
  25.  
  26. /*
  27.  * ================
  28.  * I_FloatTime
  29.  * ================
  30.  */
  31.  
  32. double          I_FloatTime()
  33. {
  34. #ifdef SYSTEM_WIN32
  35.     FILETIME        ftime;
  36.     double rval;
  37.  
  38.     GetSystemTimeAsFileTime(&ftime);
  39.  
  40.     rval = ftime.dwLowDateTime;
  41.     rval += ((__int64)ftime.dwHighDateTime) << 32;
  42.  
  43.     return (rval / 10000000.0);
  44. #endif
  45.  
  46. #ifdef SYSTEM_POSIX
  47.     struct timeval  tp;
  48.     struct timezone tzp;
  49.     static int      secbase;
  50.  
  51.     gettimeofday(&tp, &tzp);
  52.  
  53.     if (!secbase)
  54.     {
  55.         secbase = tp.tv_sec;
  56.         return tp.tv_usec / 1000000.0;
  57.     }
  58.  
  59.     return (tp.tv_sec - secbase) + tp.tv_usec / 1000000.0;
  60. #endif
  61. }
  62.  
  63. #ifdef SYSTEM_POSIX
  64. char*           strupr(char* string)
  65. {
  66.     int             i;
  67.     int             len = strlen(string);
  68.  
  69.     for (i = 0; i < len; i++)
  70.     {
  71.         string[i] = toupper(string[i]);
  72.     }
  73.     return string;
  74. }
  75.  
  76. char*           strlwr(char* string)
  77. {
  78.     int             i;
  79.     int             len = strlen(string);
  80.  
  81.     for (i = 0; i < len; i++)
  82.     {
  83.         string[i] = tolower(string[i]);
  84.     }
  85.     return string;
  86. }
  87. #endif
  88.  
  89. // Case Insensitive substring matching
  90. const char*     stristr(const char* const string, const char* const substring)
  91. {
  92.     char*           string_copy;
  93.     char*           substring_copy;
  94.     const char*     match;
  95.  
  96.     string_copy = strdup(string);
  97.     strlwr(string_copy);
  98.  
  99.     substring_copy = strdup(substring);
  100.     strlwr(substring_copy);
  101.  
  102.     match = strstr(string_copy, substring_copy);
  103.     if (match)
  104.     {
  105.         match = (string + (match - string_copy));
  106.     }
  107.  
  108.     free(string_copy);
  109.     free(substring_copy);
  110.     return match;
  111. }
  112.  
  113. char*           FlipSlashes(char* string)
  114. {
  115.     while (*string)
  116.     {
  117.         if (PATHSEPARATOR(*string))
  118.         {
  119.             *string = SYSTEM_SLASH_CHAR;
  120.         }
  121.         string++;
  122.     }
  123.     return string;
  124. }
  125.  
  126. void            DefaultExtension(char* path, const char* extension)
  127. {
  128.     char*           src;
  129.  
  130.     //
  131.     // if path doesn't have a .EXT, append extension
  132.     // (extension should include the .)
  133.     //
  134.     src = path + strlen(path) - 1;
  135.  
  136.     while (!PATHSEPARATOR(*src) && src != path)
  137.     {
  138.         if (*src == '.')
  139.             return;                                        // it has an extension
  140.         src--;
  141.     }
  142.  
  143.     strcat(path, extension);
  144. }
  145.  
  146. void            StripFilename(char* path)
  147. {
  148.     int             length;
  149.  
  150.     length = strlen(path) - 1;
  151.     while (length > 0 && !PATHSEPARATOR(path[length]))
  152.         length--;
  153.     path[length] = 0;
  154. }
  155.  
  156. void            StripExtension(char* path)
  157. {
  158.     int             length;
  159.  
  160.     length = strlen(path) - 1;
  161.     while (length > 0 && path[length] != '.')
  162.     {
  163.         length--;
  164.         if (path[length] == '/')
  165.             return;                                        // no extension
  166.     }
  167.     if (length)
  168.         path[length] = 0;
  169. }
  170.  
  171. /*
  172.  * ====================
  173.  * Extract file parts
  174.  * ====================
  175.  */
  176. void            ExtractFilePath(const char* const path, char* dest)
  177. {
  178.     hlassert (path != dest);
  179.  
  180.     const char*           src;
  181.  
  182.     src = path + strlen(path) - 1;
  183.  
  184.     //
  185.     // back up until a \ or the start
  186.     //
  187.     while (src != path && !PATHSEPARATOR(*(src - 1)))
  188.         src--;
  189.  
  190.     memcpy(dest, path, src - path);
  191.     dest[src - path] = 0;
  192. }
  193.  
  194. void            ExtractFile(const char* const path, char* dest)
  195. {
  196.     hlassert (path != dest);
  197.  
  198.     const char*           src;
  199.  
  200.     src = path + strlen(path) - 1;
  201.  
  202.     while (src != path && !PATHSEPARATOR(*(src - 1)))
  203.         src--;
  204.  
  205.     while (*src)
  206.     {
  207.         *dest++ = *src++;
  208.     }
  209.     *dest = 0;
  210. }
  211.  
  212. void            ExtractFileBase(const char* const path, char* dest)
  213. {
  214.     hlassert (path != dest);
  215.  
  216.     const char*           src;
  217.  
  218.     src = path + strlen(path) - 1;
  219.  
  220.     //
  221.     // back up until a \ or the start
  222.     //
  223.     while (src != path && !PATHSEPARATOR(*(src - 1)))
  224.         src--;
  225.  
  226.     while (*src && *src != '.')
  227.     {
  228.         *dest++ = *src++;
  229.     }
  230.     *dest = 0;
  231. }
  232.  
  233. void            ExtractFileExtension(const char* const path, char* dest)
  234. {
  235.     hlassert (path != dest);
  236.  
  237.     const char*           src;
  238.  
  239.     src = path + strlen(path) - 1;
  240.  
  241.     //
  242.     // back up until a . or the start
  243.     //
  244.     while (src != path && *(src - 1) != '.')
  245.         src--;
  246.     if (src == path)
  247.     {
  248.         *dest = 0;                                         // no extension
  249.         return;
  250.     }
  251.  
  252.     strcpy(dest, src);
  253. }
  254.  
  255. /*
  256.  * ============================================================================
  257.  * 
  258.  * BYTE ORDER FUNCTIONS
  259.  * 
  260.  * ============================================================================
  261.  */
  262.  
  263. #ifdef WORDS_BIGENDIAN
  264.  
  265. short           LittleShort(const short l)
  266. {
  267.     byte            b1, b2;
  268.  
  269.     b1 = l & 255;
  270.     b2 = (l >> 8) & 255;
  271.  
  272.     return (b1 << 8) + b2;
  273. }
  274.  
  275. short           BigShort(const short l)
  276. {
  277.     return l;
  278. }
  279.  
  280. int             LittleLong(const int l)
  281. {
  282.     byte            b1, b2, b3, b4;
  283.  
  284.     b1 = l & 255;
  285.     b2 = (l >> 8) & 255;
  286.     b3 = (l >> 16) & 255;
  287.     b4 = (l >> 24) & 255;
  288.  
  289.     return ((int)b1 << 24) + ((int)b2 << 16) + ((int)b3 << 8) + b4;
  290. }
  291.  
  292. int             BigLong(const int l)
  293. {
  294.     return l;
  295. }
  296.  
  297. float           LittleFloat(const float l)
  298. {
  299.     union
  300.     {
  301.         byte            b[4];
  302.         float           f;
  303.     }
  304.     in             , out;
  305.  
  306.     in.f = l;
  307.     out.b[0] = in.b[3];
  308.     out.b[1] = in.b[2];
  309.     out.b[2] = in.b[1];
  310.     out.b[3] = in.b[0];
  311.  
  312.     return out.f;
  313. }
  314.  
  315. float           BigFloat(const float l)
  316. {
  317.     return l;
  318. }
  319.  
  320. #else // Little endian (Intel, etc)
  321.  
  322. short           BigShort(const short l)
  323. {
  324.     byte            b1, b2;
  325.  
  326.     b1 = (byte) (l & 255);
  327.     b2 = (byte) ((l >> 8) & 255);
  328.  
  329.     return (short)((b1 << 8) + b2);
  330. }
  331.  
  332. short           LittleShort(const short l)
  333. {
  334.     return l;
  335. }
  336.  
  337. int             BigLong(const int l)
  338. {
  339.     byte            b1, b2, b3, b4;
  340.  
  341.     b1 = (byte) (l & 255);
  342.     b2 = (byte) ((l >> 8) & 255);
  343.     b3 = (byte) ((l >> 16) & 255);
  344.     b4 = (byte) ((l >> 24) & 255);
  345.  
  346.     return ((int)b1 << 24) + ((int)b2 << 16) + ((int)b3 << 8) + b4;
  347. }
  348.  
  349. int             LittleLong(const int l)
  350. {
  351.     return l;
  352. }
  353.  
  354. float           BigFloat(const float l)
  355. {
  356.     union
  357.     {
  358.         byte            b[4];
  359.         float           f;
  360.     }
  361.     in             , out;
  362.  
  363.     in.f = l;
  364.     out.b[0] = in.b[3];
  365.     out.b[1] = in.b[2];
  366.     out.b[2] = in.b[1];
  367.     out.b[3] = in.b[0];
  368.  
  369.     return out.f;
  370. }
  371.  
  372. float           LittleFloat(const float l)
  373. {
  374.     return l;
  375. }
  376.  
  377. #endif
  378.  
  379. //=============================================================================
  380.  
  381. bool CDECL      safe_snprintf(char* const dest, const size_t count, const char* const args, ...)
  382. {
  383.     size_t          amt;
  384.     va_list         argptr;
  385.  
  386.     hlassert(count > 0);
  387.  
  388.     va_start(argptr, args);
  389.     amt = vsnprintf(dest, count, args, argptr);
  390.     va_end(argptr);
  391.  
  392.     // truncated (bad!, snprintf doesn't null terminate the string when this happens)
  393.     if (amt == count)
  394.     {
  395.         dest[count - 1] = 0;
  396.         return false;
  397.     }
  398.  
  399.     return true;
  400. }
  401.  
  402. bool            safe_strncpy(char* const dest, const char* const src, const size_t count)
  403. {
  404.     return safe_snprintf(dest, count, "%s", src);
  405. }
  406.  
  407. bool            safe_strncat(char* const dest, const char* const src, const size_t count)
  408. {
  409.     if (count)
  410.     {
  411.         strncat(dest, src, count);
  412.  
  413.         dest[count - 1] = 0;                               // Ensure it is null terminated
  414.         return true;
  415.     }
  416.     else
  417.     {
  418.         Warning("safe_strncat passed empty count");
  419.         return false;
  420.     }
  421. }
  422.  
  423. bool            TerminatedString(const char* buffer, const int size)
  424. {
  425.     int             x;
  426.  
  427.     for (x = 0; x < size; x++, buffer++)
  428.     {
  429.         if ((*buffer) == 0)
  430.         {
  431.             return true;
  432.         }
  433.     }
  434.     return false;
  435. }
  436.